<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, student-scalable=no"/>
    <title>${siteName!""}|图书管理-添加图书</title>
    <#include "../common/header.ftl"/>

</head>

<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <aside class="lyear-layout-sidebar">

            <div class="lyear-layout-sidebar-scroll">
                <#include "../common/left-menu.ftl"/>
            </div>

        </aside>
        <!--End 左侧导航-->

        <#include "../common/header-menu.ftl"/>

        <!--页面主要内容-->
        <main class="lyear-layout-content">

            <div class="container-fluid">

                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-header"><h4>添加图书</h4></div>
                            <div class="card-body">
                                <form action="add" id="student-add-form" method="post" class="row">

                                    <div class="input-group m-b-10">
                                        <span class="input-group-addon">图书名称</span>
                                        <input type="text" class="form-control required" id="bookName" name="bookName"
                                               value="" placeholder="请输入图书名称" tips="请填写图书名称"/>
                                    </div>
                                    <div class="input-group m-b-10">
                                        <span class="input-group-addon">作者</span>
                                        <input type="text" class="form-control required" id="author" name="author"
                                               value="" placeholder="请填写图书作者" tips="请填写图书作者"/>
                                    </div>

                                    <div class="input-group m-b-10">
                                        <span class="input-group-addon">单价</span>
                                        <input type="text" class="form-control required" id="price" name="price"
                                               value="" placeholder="请填写图书单价" tips="请填写图书单价"/>
                                    </div>

                                    <div class="input-group m-b-10">
                                        <span class="input-group-addon">出版社</span>
                                        <input type="text" class="form-control required" id="press" name="press"
                                               value="" placeholder="请填写出版社" tips="请填写出版社"/>
                                    </div>

                                    <div class="input-group m-b-10">
                                        <span class="input-group-addon">所属分类</span>
                                        <select name="parent.id" class="form-control" id="category"
                                                onchange="changeCate()">
                                            <option value="0">---请选择---</option>
                                            <#list categories as category>
                                                <option value="${category.id}">${category.typeName}</option>
                                            </#list>
                                        </select>
                                    </div>
                                    <div class="input-group m-b-10">
                                        <span class="input-group-addon">所属二级分类</span>
                                        <select name="category" class="form-control" id="parent">

                                        </select>
                                    </div>

                                    <div class="input-group m-b-10">
                                        <span class="input-group-addon">图书库存</span>
                                        <input type="number" class="form-control required" id="cannum" name="cannum"
                                               value="" placeholder="请输入库存数量" tips="请填写库存数量" min="1" onchange="if(/\D/.test(this.value)){this.value='1';}"/>
                                    </div>
                                    <div class="input-group m-b-10">
                                        <span class="input-group-addon">简介</span>
                                        <input type="text" class="form-control required" id="introduce" name="introduce"
                                               value="" placeholder="请输入图书简介" tips="请填写图书简介"/>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <button type="button" class="btn btn-primary ajax-post"
                                                id="add-form-submit-btn">确 定
                                        </button>
                                        <button type="button" class="btn btn-default"
                                                onclick="javascript:history.back(-1);return false;">返 回
                                        </button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <!--End 页面主要内容-->
    </div>
</div>
<#include "../common/footer.ftl"/>
<script type="text/javascript" src="/admin/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="/admin/js/main.min.js"></script>

<script type="text/javascript" src="/admin/js/perfect-scrollbar.min.js"></script>

<script type="text/javascript" src="/admin/js/main.min.js"></script>

<script type="text/javascript">
    //选择一级分类 触发的函数
    function changeCate() {
        var categoryId = $("#category option:selected").val();
        $.ajax({
            url: "category",
            type: "POST",
            dataType: "json",
            data: {
                id:categoryId,
            },
            success: function (data) {
                console.log(data);
                var html = '<option value=\'\'>---请选择---</option>';
                $("#parent").empty();
                for (var i = 0, len = data.length; i < len; i++) {
                    html += "<option value='" + data[i].id +"'> " + data[i].typeName + "</option>";
                }
                $("#parent").append(html);


            },
            error: function (data) {
                // alert('网络错误!');
            }
        });
    }


    $(document).ready(function () {

        //提交按钮监听事件
        $("#add-form-submit-btn").click(function () {
            if (!checkForm("student-add-form")) {
                return;
            }


            var data = $("#student-add-form").serialize();
            $.ajax({
                url: 'add',
                type: 'POST',
                data: data,
                dataType: 'json',
                success: function (data) {
                    if (data.code == 0) {
                        showSuccessMsg('图书添加成功!', function () {
                            window.location.href = 'list';
                        })
                    } else {
                        showErrorMsg(data.msg);
                    }
                },
                error: function (data) {
                    alert('网络错误!');
                }
            });
        });
        //监听上传图片按钮
        $("#add-pic-btn").click(function () {
            $("#select-file").click();
        });

    });

</script>


</body>
</html>
